package www.ld.com.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.ld.com.domain.R;
import www.ld.com.domain.resources.pojo.ResourcePO;
import www.ld.com.domain.vo.SelectTreeVO;
import www.ld.com.service.ResourceService;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("resources")
@Api(tags = "系统资源")
public class ResourceController {
    @Autowired
    private ResourceService resourcesService;

    @ApiOperation(value = "资源树形机构")
    @GetMapping("tree_resources")
    public R<List<SelectTreeVO>> treeResources() {
        //获取资源
        List<ResourcePO> resources = resourcesService.findResources();
        List<SelectTreeVO> selectTrees = resources.stream().map(v -> {
            SelectTreeVO selectTreeVO = new SelectTreeVO();
            selectTreeVO.setParentId(v.getParentsCode()).setText(v.getName()).setValue(v.getCode());
            return selectTreeVO;
        }).collect(Collectors.toList());
        return R.ok(selectTrees);
    }
}
